1907G - Lights - CodeForces Solution


constructive algorithms dfs and similar graphs greedy implementation

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

void solve(){
    int N;
    string S;
    cin>>N>>S;
    vector<int>A(N),ind(N);
    vector<bool>flag(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
        A[i]--;
        ind[A[i]]++;
        if(S[i]=='1')flag[i]=true;
    }
    queue<int>q;
    for(int i=0;i<N;i++){
        if(ind[i]==0)q.push(i);
    }
    vector<int>ans;
    while(q.size()){
        int x=q.front();
        q.pop();
        if(ind[A[x]]>0){
            ind[A[x]]--;
            if(flag[x]){
                flag[x]=false;
                flag[A[x]]=!flag[A[x]];
                ans.push_back(x+1);
            }
            if(ind[A[x]]==0){
                q.push(A[x]);
            }
        }
    }
    for(int i=0;i<N;i++){
        if(ind[i]>0&&flag[i]){
            int at=i;
            bool light=true;
            int x=1,y=1;
            while(1){
                at=A[at];
                if(at==i)break;
                if(flag[at]){
                    light=!light;
                }
                if(light)x++;
                y++;
                ind[at]--;
            }
            if(light){
                cout<<-1<<"\n";
                return;
            }
            light=true;
            while(1){
                if(light==(x<=y-x)){
                    ans.push_back(at+1);
                }
                at=A[at];
                if(at==i)break;
                if(flag[at]){
                    light=!light;
                }
            }
        }
    }
    cout<<(int)ans.size()<<"\n";
    for(auto a:ans)cout<<a<<" ";
    cout<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin>>T;
    while(T--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST